#include "comm.hpp"
#include "log.hpp"

int main()
{
    key_t key=ftok(PATH_NAME,proj_ID);
    if(key==-1)
    {
        //perror("client ftok");
        logMessage(FATAL,"%s [%s][%d]","client ftok fatal",__FILE__,__LINE__);
        exit(1);
    }

    //获取shmid
    int shmid=shmget(key,SIZE,0);
    if(shmid==-1)
    {
        //perror("client shmget");
        logMessage(ERROR,"%s [%s][%d]","client shmget error",__FILE__,__LINE__);
        exit(2);
    }
    logMessage(NORMAL,"%s [%s][%d]","clientshmget success",__FILE__,__LINE__);

    //挂接共享内存
    char* shmaddr=(char*)shmat(shmid,nullptr,0);
    logMessage(NORMAL,"%s [%s][%d]","client shmat success",__FILE__,__LINE__);

    //读取逻辑
    //int count=20;
    int fd=OpenFifo(FIFO_NAME,READ);
    while(true)
    {
        Wait(fd);
        logMessage(NORMAL,"%s %s [%s][%d]","client read success",shmaddr,__FILE__,__LINE__);
        if(strcmp(shmaddr,"quit")==0)
        {
            break;
        }
        // if(!count--)
        // {
        //     break;
        // }
        //sleep(1);
    }
    //取消挂接共享内存
    int n=shmdt(shmaddr);
    if(n==-1)
    {
        //perror("client shmdt");
        logMessage(ERROR,"%s [%s][%d]","client shmdt error",__FILE__,__LINE__);
        exit(3);
    }
    logMessage(NORMAL,"%s [%s][%d]","client shmdt success",__FILE__,__LINE__);
    CloseFifo(fd);
    return 0;
}